package com.study.jyg.rxjava2study.activity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.base.BasicActivity;
import com.study.jyg.rxjava2study.event.GetStartAppTimeEvent;
import com.study.jyg.rxjava2study.event.StartAppTimeEvent;
import com.study.jyg.rxjava2study.service.StartAppService;
import com.study.jyg.rxjava2study.utils.MyMath;
import com.study.jyg.rxjava2study.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

/**
 * 钉钉打卡页面
 */
public class DingdingActivity extends BasicActivity {

    private TextView tv1;
    private Handler mHandler;
    private Runnable mRunnable;
    private int rondom;

    @Override
    protected int getLayout() {
        return R.layout.activity_dingding;
    }

    @Override
    protected void initView() {
        tv1 = findViewById(R.id.tv1);
        EventBus.getDefault().register(this);
//        if (!isServiceRunning(mContext, ".service.StartAppService")){
//            startService(new Intent(mContext, StartAppService.class));
//        }else {
//            EventBus.getDefault().post(new GetStartAppTimeEvent());
//        }
        initHandler();
    }

    private void initHandler(){
        rondom = MyMath.random(9);
        tv1.setText(String.format("下次打卡时间：08:5%s",rondom));
        EventBus.getDefault().post(new StartAppTimeEvent(rondom));
        mHandler = new Handler();
        mRunnable = () -> {
            Log.e("rondom", "rondom = " + rondom);
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            if (hour == 8 && minute == 50 + rondom && week != 1) {
                rondom = MyMath.random(9);
                tv1.setText(String.format("下次打卡时间：08:5%s",rondom));
                try {
                    Intent intent = getPackageManager().getLaunchIntentForPackage("com.alibaba.android.rimet");
                    startActivity(intent);
                } catch (Exception e) {
                    ToastUtils.show("没有安装DingDingApp");
                }
            }
            mHandler.postDelayed(mRunnable, 1000);
        };
        mHandler.post(mRunnable);
    }

    @Subscribe
    public void getRondom(StartAppTimeEvent e){
        tv1.setText(String.format("下次打卡时间：08:5%s",e.getMinute()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null)
            mHandler.removeCallbacks(mRunnable);
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }

    public static Boolean isServiceRunning(Context context, String serviceName) {
        //获取服务方法  参数 必须用大写的Context！！！
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> infos = am.getRunningServices(100);
        for (ActivityManager.RunningServiceInfo info : infos) {
            String className = info.service.getShortClassName();
            if(serviceName.equals(className))
                return true;
        }
        return false;
    }
}